local qubing = fk.CreateSkill {
    name = "kq__qubing",
    tags = { Skill.Switch },
}

local U = require "packages/utility/utility"

qubing:addEffect(fk.EventPhaseStart, {
anim_type = "switch",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(qubing.name) and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local state = player:getSwitchSkillState(qubing.name)
    if state == fk.SwitchYin then
        local choice = room:askToChoice(player, {
          choices = {"#beishui" , "Cancel"},
          skill_name = qubing.name
        })
        if choice == "#beishui" then
            room:addPlayerMark(player, "@@kq__qubing-turn")
            local duel = Fk:cloneCard("lure_tiger")
            local max_num = duel.skill:getMaxTargetNum(player, duel)
            local targets = table.filter(room:getOtherPlayers(player, false), function (p)
            return player:canUseTo(duel, p)
            end)
            if #targets > 0 then
                local tos = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = max_num,
                targets = targets,
                skill_name = qubing.name,
                prompt = "#qubing2",
                cancelable = false,
                })
                if #tos > 0 then
                    local targets = tos
                    room:sortByAction(targets)
                room:useVirtualCard("lure_tiger", nil, player, targets, qubing.name, true)
                end
            end

            U.SetSwitchSkillState(player, qubing.name, player:getSwitchSkillState(qubing.name, false) == fk.SwitchYang and fk.SwitchYin or fk.SwitchYang)

        end
        local duel = Fk:cloneCard("known_both")
        local max_num = duel.skill:getMaxTargetNum(player, duel)
        local targets = table.filter(room:getOtherPlayers(player, false), function (p)
        return player:canUseTo(duel, p)
        end)
        if #targets > 0 then
            local tos = room:askToChoosePlayers(player, {
            min_num = 1,
            max_num = max_num,
            targets = targets,
            skill_name = qubing.name,
            prompt = "#qubing1",
            cancelable = false,
            })
            if #tos > 0 then
                local targets = tos
                room:sortByAction(targets)
                room:useVirtualCard("known_both", nil, player, targets, qubing.name, true)
            end
        end
        else if state == fk.SwitchYang then
            local choice = room:askToChoice(player, {
            choices = {"#beishui" , "Cancel"},
            skill_name = qubing.name
            })
            if choice == "#beishui" then
                room:addPlayerMark(player, "@@kq__qubing-turn")
                local duel = Fk:cloneCard("known_both")
                local max_num = duel.skill:getMaxTargetNum(player, duel)
                local targets = table.filter(room:getOtherPlayers(player, false), function (p)
                return player:canUseTo(duel, p)
                end)
                if #targets > 0 then
                    local tos = room:askToChoosePlayers(player, {
                    min_num = 1,
                    max_num = max_num,
                    targets = targets,
                    skill_name = qubing.name,
                    prompt = "#qubing1",
                    cancelable = false,
                    })
                    if #tos > 0 then
                        local targets = tos
                        room:sortByAction(targets)
                        room:useVirtualCard("known_both", nil, player, targets, qubing.name, true)
                    end
                end

                U.SetSwitchSkillState(player, qubing.name, player:getSwitchSkillState(qubing.name, false) == fk.SwitchYang and fk.SwitchYin or fk.SwitchYang)

            end
            local duel = Fk:cloneCard("lure_tiger")
            local max_num = duel.skill:getMaxTargetNum(player, duel)
            local targets = table.filter(room:getOtherPlayers(player, false), function (p)
            return player:canUseTo(duel, p)
            end)
            if #targets > 0 then
                local tos = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = max_num,
                targets = targets,
                skill_name = qubing.name,
                prompt = "#qubing2",
                cancelable = false,
                })
                if #tos > 0 then
                    local targets = tos
                    room:sortByAction(targets)
                room:useVirtualCard("lure_tiger", nil, player, targets, qubing.name, true)
                end
            end
        end
    end
  end,
})

qubing:addEffect("prohibit", {
  prohibit_use = function(self, player, card)
    return card and player:getMark("@@kq__qubing-turn") > 0 and card.type == Card.TypeEquip
  end,
})

qubing:addEffect(fk.DamageCaused, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return
      target == player and
      player:hasSkill(qubing.name) and
      player:getMark("@@kq__qubing-turn") > 0 and
      data.damageType ~= fk.NormalDamage and
      data.to == player
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data:changeDamage(1)
  end,
})

Fk:loadTranslationTable {
    ["kq__qubing"] = "渠兵",
    [":kq__qubing"] = "转换技，准备阶段，你可以视为使用一张①【知己知彼】；②【调虎离山】。<br>"..
    "背水：本回合你不能使用装备牌且受到的属性伤害+1。",
    ["#qubing1"] = "渠兵：你可以视为使用一张【知己知彼】",
    ["#qubing2"] = "渠兵：你可以视为使用一张【调虎离山】",
    ["#beishui"] = "背水：本回合你不能使用装备牌且受到的属性伤害+1",
    ["@@kq__qubing-turn"] = "受到属性伤害+1<br>"..
    "无法使用装备牌"
}

return qubing